package testWater.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.perye.dokit.utils.FileUtil;
import com.perye.dokit.utils.PageUtil;
import com.perye.dokit.utils.QueryHelp;
import com.perye.dokit.utils.ValidationUtil;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import testWater.entity.TestWater;
import testWater.dto.TestWaterDto;
import testWater.mapper.TestWaterMapper;
import testWater.query.TestWaterQueryCriteria;
import testWater.repository.TestWaterRepository;
import testWater.service.TestWaterService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * 业务层实现
 *
 * @author: water
 * @date: 2020-11-02
 * @description:
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TestWaterServiceImpl implements TestWaterService {

    private final TestWaterRepository repository;

    private final TestWaterMapper mapper;

    public TestWaterServiceImpl(TestWaterRepository repository, TestWaterMapper mapper) {
        this.repository = repository;
        this.mapper = mapper;
    }

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(TestWaterQueryCriteria criteria, Pageable pageable) {
        // Page<TestWater> page = repository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        // return PageUtil.toPage(page.map(mapper::toDto));
        Map<String, Object> map = new HashMap<>();
        return map;
    }

    @Override
    //@Cacheable
    public List<TestWaterDto> queryAll(TestWaterQueryCriteria criteria) {
        //return mapper.toDto(repository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
        List<TestWaterDto> list = new ArrayList<>();
        return list;
    }

    @Override
    //@Cacheable(key = "#p0")
    public TestWaterDto findById(Long id) {
        TestWater TestWater = repository.findById(id).orElseGet(TestWater::new);
        ValidationUtil.isNull(TestWater.getId(), "TestWater", "id", id);
        return mapper.toDto(TestWater);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public TestWaterDto create(TestWater resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId());
        return mapper.toDto(repository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(TestWater resources) {
        TestWater TestWater = repository.findById(resources.getId()).orElseGet(TestWater::new);
        ValidationUtil.isNull(TestWater.getId(), "TestWater", "id", resources.getId());
        TestWater.copy(resources);
        repository.save(TestWater);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            repository.deleteById(id);
        }
    }

    @Override
    public void download(List<TestWaterDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TestWaterDto TestWater : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put(" name", TestWater.getName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

}
